<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <el-form ref="form" :model="pageData" label-width="80px" size="small">
          <el-form-item label="学科名称">
            <el-input
              v-model="pageData.subjectName"
              style="width: 200px; flex: true"
            ></el-input>
            <el-button style="margin-left: 20px" @click="clear">清除</el-button>
            <el-button type="primary" @click="onSubmit">搜索</el-button>
            <el-button
              type="success"
              icon="el-icon-edit"
              style="margin-left: 700px"
              @click="addSubject"
              >新增学科</el-button
            >
          </el-form-item>
        </el-form>
        <el-alert
          :title="`共有${counts}条记录`"
          type="info"
          show-icon
          :closable="false"
        >
        </el-alert>
        <el-table :data="list" style="width: 100%">
          <el-table-column label="序号" type="index"> </el-table-column>
          <el-table-column prop="subjectName" label="学科名称">
          </el-table-column>
          <el-table-column prop="username" label="创建者"> </el-table-column>
          <el-table-column prop="addDate" label="创建日期" width="250px">
            <template #default="{ row }">
              <span>{{ formatDate(row.addDate) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="isFrontDisplay" label="前台是否显示">
            <template #default="{ row }">
              <span>{{ block[row.isFrontDisplay] }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="twoLevelDirectory" label="二级目录">
          </el-table-column>
          <el-table-column prop="tags" label="标签"> </el-table-column>
          <el-table-column prop="totals" label="题目数量"> </el-table-column>
          <el-table-column label="操作" width="250px">
            <template #default="{ row }">
              <el-button type="text" @click="goClass(row)">学科分类</el-button>
              <el-button type="text" @click="goLabel(row)">学科标签</el-button>
              <el-button type="text" @click="revise(row)">修改</el-button>
              <el-button type="text" @click="remove(row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="end">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :page-sizes="[5, 10, 20, 50]"
            layout=" prev, pager, next,sizes,  jumper"
            :total="counts"
            background
          >
          </el-pagination>
        </div>
      </el-card>
      <el-dialog
        :title="ruleForm.id ? '编辑学科' : '新增学科'"
        :visible="dialogVisible"
        width="30%"
        @close="closeDialog"
      >
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
        >
          <el-form-item label="学科名称" prop="subjectName">
            <el-input
              v-model="ruleForm.subjectName"
              placeholder="请输入学科名称"
            ></el-input>
          </el-form-item>
          <el-form-item label="是否显示">
            <el-switch
              v-model="ruleForm.isFrontDisplay"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
            >
            </el-switch>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="submitSubject">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { list, add, update, remove } from '@/api/hmmm/subjects.js'
// import dayjs from 'dayjs'
export default {
  name: 'jiajun',
  async created () {
    this.getList()
  },
  data () {
    return {
      // 渲染的数据
      list: [],
      // 总条数
      counts: 0,
      // 弹窗数据
      ruleForm: {
        subjectName: '',
        isFrontDisplay: 1
      },
      // 分页
      pageData: {
        page: 1,
        pagesize: 10,
        subjectName: ''
      },
      // 渲染
      block: {
        1: '是',
        0: '否'
      },
      // 弹窗显示与隐藏
      dialogVisible: false,
      // 表单验证规则
      rules: {
        subjectName: [
          { required: true, message: '请输入学科名称', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 搜索提交按钮
    onSubmit () {
      // console.log("submit!");
      this.pageData.page = 1
      this.getList(this.pageData)
    },
    // 搜索框清除
    clear () {
      this.pageData.subjectName = ''
    },
    // 每页多少条数据
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`);
      this.pageData.pagesize = val
      this.pageData.page = 1
      this.getList()
    },
    // 当前页
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`);
      this.pageData.page = val
      this.getList()
    },
    // 获取当前数据
    async getList () {
      // this.pageData.page = 1;
      const { data } = await list(this.pageData)
      this.list = data.items
      this.counts = data.counts
    },
    // 渲染时间
    formatDate (time) {
      // return dayjs(time).format('YYYY-MM-DD HH:MM:ss')
    },
    // 新增学科打开弹窗
    addSubject () {
      this.dialogVisible = true
    },
    // 关闭弹窗时
    closeDialog () {
      this.dialogVisible = false
      // 重置表单;
      this.$refs.ruleForm.resetFields()
      this.ruleForm = { subjectName: '', isFrontDisplay: 1 }
    },
    // 添加学科
    async submitSubject () {
      const msg = this.ruleForm.id ? '编辑学科成功' : '添加学科成功'
      if (this.ruleForm.id) {
        // 编辑学科
        await update(this.ruleForm)
      } else {
        // 添加学科
        await add(this.ruleForm)
      }
      // 提示用户
      this.$message.success(msg)
      // 关闭弹窗
      this.closeDialog()
      this.getList()
    },
    // 修改学科
    revise (row) {
      // await update(a)
      // console.log(a);
      this.ruleForm = row
      this.dialogVisible = true
    },
    // 删除学科
    remove (row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove(row)
        if (this.list.length === 1) {
          this.pageData.page = 1
        }
        this.$message.success('删除成功')
        this.getList()
      })
    },
    // 去目录页
    goClass (row) {
      this.$router.push({
        path: '/subjects/directorys',
        query: {
          id: row.id,
          name: row.subjectName
        }
      })
    },
    // 去标签页
    goLabel (row) {
      this.$router.push({
        path: '/subjects/tags',
        query: {
          id: row.id,
          name: row.subjectName
        }
      })
    }
  }
}
</script>

<style scoped>
.end {
  margin-left: 750px;
  width: 100%;
  margin-top: 10px;
}
</style>
